package com.liyou.process.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liyou.model.process.ProcessTemplate;
import com.liyou.model.process.ProcessType;
import com.liyou.process.service.OaProcessService;
import com.liyou.process.service.OaProcessTemplateService;
import com.liyou.process.mapper.OaProcessTemplateMapper;
import com.liyou.process.service.OaProcessTypeService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
* @author asus
* @description 针对表【oa_process_template(审批模板)】的数据库操作Service实现
* @createDate 2023-06-09 17:00:31
*/
@Service
public class OaProcessTemplateServiceImpl extends ServiceImpl<OaProcessTemplateMapper, ProcessTemplate>
    implements OaProcessTemplateService{

    @Resource
    private OaProcessTypeService oaProcessTypeService;

    @Resource
    private OaProcessService oaProcessService;

    @Override
    public IPage<ProcessTemplate> selectPageProcessTempate(Page<ProcessTemplate> pageParam) {

        //1 进行原表分页查询
        Page<ProcessTemplate> processTemplatePage = baseMapper.selectPage(pageParam, null);

        //2 从分页的数据中获取list集合
        List<ProcessTemplate> processTemplateList = processTemplatePage.getRecords();

        //3 遍历list集合，得到每个对象的审批类型id
        for (ProcessTemplate processTemplate : processTemplateList) {
            Long processTypeId = processTemplate.getProcessTypeId();

            //4 根据审批类型id查询对应名称
            LambdaQueryWrapper<ProcessType> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ProcessType::getId, processTypeId);
            ProcessType processType = oaProcessTypeService.getOne(wrapper);
            if (processType == null) {
                continue;
            }
            //5 重新封装processTypeName
            processTemplate.setProcessTypeName(processType.getName());
        }

        //6 返回新的分页结果
        return processTemplatePage;
    }

    @Override
    public void publish(Long id) {
        //修改模板的发布状态 1已经发布 0未发布
        ProcessTemplate processTemplate = baseMapper.selectById(id);
        processTemplate.setStatus(1);
        baseMapper.updateById(processTemplate);

        //流程定义部署
        if (StringUtils.isEmpty(processTemplate.getProcessDefinitionPath())) {
            oaProcessService.deployByZip(processTemplate.getProcessDefinitionPath());
        }
    }
}




